<?php
class group extends common{
	
	function __construct(){	
		$pagec = new page($_GET['page']);
		parent::__construct($pagec);
	}	
	private $table_admin_group = 'admin_group';	//角色表
	private $table_admin = 'admin';				//管理员表
	private $table_right = 'admin_group_right';	//权限表

	private $table_topic = 'admin_topic';			//小编说
	private $table_comment = 'admin_topic_comment';		//小编回复
	private $table_user = 'user';					//用户表

	/**
	 * 管理员列表
	 * @author	jsy
	 * @param	string	$where  
	 * @param	string	$key 
	 * @param	int	$is_page  
	 * @return 	array
	 */

	function admin_list($where='',$key="a.*,g.group_name",$is_page=false){
		$sql = $this->table_admin." a LEFT JOIN ".$this->table_admin_group." g ON a.group_id=g.id WHERE 1=1 AND a.id!=1 ".$where." order by a.id" ;
		if($is_page){
			$sql_num = $sql;
		}else{
			$sql_num = "";
		}
		$res = $this->getList($sql,$sql_num,$key);
		return $res ;
	}


	/**
	 * 角色列表
	 * @author	jsy
	 * @param	string	$where
	 * @param	string	$key 
	 * @param	int	$is_page  默认为有分页1，2为无分页
	 * @return 	array
	 */
	function admin_group_list($where='',$key="a.*",$is_page=false){
		$sql = $this->table_admin_group." a WHERE 1=1 ".$where;
		if($is_page){
			$sql_num = $sql;
		}else{
			$sql_num = "";
		}
		$res = $this->getList($sql,$sql_num,$key);
		return $res ;
	}


	/**
	 * 权限添加
	 * @author	jsy	
	 * @param	array	$arr_class  //单条数组
	 * @param	array	$arr 		//批量数组
	 * @return 	bool
	 */
	function right_add($arr_class,$arr){
		if( !empty($arr_class)){
		
			$res = $this->insert($this->table_admin_group,$arr_class);	//插入活动
			$id = $this->insert_id();	

			if($id>0 && !empty($arr)){
				$sql = ' INSERT INTO '.$this->table_right. '(group_id,right_id) VALUES ';
				foreach($arr as $v){
					$sql.='(';
					$sql.= $id.",".$v['right_id'];
					$sql .= '),';
				}
				$sql = substr($sql,0,-1);
				$res = $this->query($sql);
			}	
		}
		return $res;
	}

	/**
	 * 权限修改
	 * @author	jsy
	 * @param 	int	 	$id
	 * @param	array	$arr_class  //单条数组
	 * @param	array	$arr 		//批量数组
	 * @return	bool
	 */
	function right_update($id,$arr_class,$arr){
		if( is_array($arr_class)){
			$res=$this->update($this->table_admin_group,$arr_class,"id=".$id);
			//删除原有数据
			$res1 = $this->del($this->table_right,$id.',','',"group_id");

			if($id && !empty($arr)){
				$sql = ' INSERT INTO '.$this->table_right. '(group_id,right_id) VALUES ';
				foreach($arr as $v){
					$sql.='(';
					$sql.= $id.",".$v['right_id'];
					$sql .= '),';
				}
				$sql = substr($sql,0,-1);
				$res = $this->query($sql);
			}	
		}
		return $res;
	}


	/********话题类**************/
	/**
	 * 小编说列表
	 * @author	jsy
	 * @param	string	$where  
	 * @param	string	$key 
	 * @param	int	$is_page  
	 * @return 	array
	 */
	function topic_list($where='',$key="a.*",$is_page=false){
		$sql = $this->table_topic." a WHERE 1=1  ".$where." order by a.create_time desc" ;
		if($is_page){
			$sql_num = $sql;
		}else{
			$sql_num = "";
		}
		$res = $this->getList($sql,$sql_num,$key);
		return $res ;
	}

	/**
	 * 小编说详情
	 * @author	jsy
	 * @param	int	    $id
	 * @param	string	$key
	 * @return	array
	 */
	function topic_info($id,$key="*"){
		if($id != null && $id != ""){
			$row = $this->getInfo($this->table_topic,$id,$key,'id');		
		}
		return $row;
	}
	

	/**
	 * 小编回复
	 * @author	jsy
	 * @param	int		$id  
	 * @param	string	$where  
	 * @param	string	$key 
	 * @param	int	$is_page  
	 * @return 	array
	 */
	function comment_list($id,$where='',$key="a.*,u.account,u1.account as account1",$is_page=false){
		$sql = $this->table_comment." a LEFT JOIN ".$this->table_user." u ON a.user_id=u.id  LEFT JOIN ".$this->table_user." u1 ON a.reply_id=u1.id WHERE a.topic_id=".$id.$where." order by a.id desc" ;
		if($is_page){
			$sql_num = $sql;
		}else{
			$sql_num = "";
		}
		$res = $this->getList($sql,$sql_num,$key);
		return $res ;
	}


}
?>